/**
 * @param {number[]} prices
 * @return {number}
 */
// 121. 买卖股票的最佳时机
/* 动态规划 */
var maxProfit = function (prices) {
    if (prices.length <= 1) return 0
    let minPrice = prices[0], maxPrice = 0
    for (let i = 1; i < prices.length; i++) {
        maxPrice = Math.max(maxPrice, prices[i] - minPrice)
        minPrice = Math.min(minPrice, prices[i])
    }

    return maxPrice
}

var maxProfit = function (prices) {
    if (!prices.length) return 0
    let maxPrice = 0
    let minPrice = prices[0]
    for (let i = 1; i < prices.length; i++) {
        if (prices[i] < minPrice) {
            minPrice = prices[i]
        } else if (prices[i] - minPrice > maxPrice) {
            maxPrice = prices[i] - minPrice
        }
    }

    return maxPrice
}

// 
const max = maxProfit([7, 6, 4, 3, 1])
console.log("maxProfit :>>", max);